home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / irix / tools / chktime.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.4 KB  |  171 lines

  1. /*----------------------------------------------------------------------
  2.  * $Header: /d1/dist/tools/RCS/chktime.c,v 1.2 1991/07/19 15:29:02 carlson Exp $
  3.  *
  4.  * chktime.c    Compares two files modification times and returns -1, 0
  5.  *         or +1 depending on whether the first file was modified
  6.  *        before, at the same time or after the second file.
  7.  *
  8.  * Synopsis:
  9.  *    chktime [-d] [-r] <source-file> <object-file | executable-file>
  10.  *
  11.  * Description:
  12.  *    The purpose of this routine is to determine if a particular
  13.  *    source file needs to be recompiled.  Since MCS does not use
  14.  *    the features of make to determine this need, this routine
  15.  *    had to be written.
  16.  *
  17.  *    The functions performed by chktime are as follows:
  18.  *    1.  If -r option given, skip to step 3.
  19.  *    2.  If the source file exists in the current directory:
  20.  *        a.  Set MODTIM to the last modified time of the file.
  21.  *        b.  Skip to step 5.
  22.  *    3.  If the source file does not exist in ./RCS as an RCS
  23.  *        file, exit with an error message.
  24.  *    4.  Set MODTIM to the last modified time of the latest
  25.  *        revision in the RCS file.
  26.  *    5.  If MODTIM is less than or equal to the modification
  27.  *        time of the object file, return with a -1 or 0.
  28.  *    6.  Return with status of 1.
  29.  *
  30.  *    The option -d will enable debug output.
  31.  *
  32.  * Revision history:
  33.  *    $Log: chktime.c,v $
  34.  * Revision 1.2  1991/07/19  15:29:02  carlson
  35.  * Added Header for RCS.  Fixed up documentation.
  36.  *
  37.  * Revision 1.1  90/10/09  17:14:11  chris
  38.  * Initial revision
  39.  * 
  40.  * 15 Aug 1989  Christopher W. Carlson, Silicon Graphics Inc.
  41.  * Initial draft
  42.  *
  43.  *--------------------------------------------------------------------*/
  44.  
  45. #include <stdio.h>
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #include <errno.h>
  49. #include <time.h>
  50. #include <ctype.h>
  51.  
  52. static struct stat s_stat, o_stat;
  53. static char cbuff[256], dbuff[256];
  54. static enum { FALSE, TRUE } debug, rcs_only;
  55.  
  56. main (argc, argv)
  57.   int argc;
  58.   char *argv[];
  59. {
  60.     register int i, j;
  61.     register char *cpos;
  62.     register struct tm *tmptr;
  63.     FILE *fp;
  64.  
  65.     i = 3;            /* Expected number of parameters */
  66.     debug = FALSE;        /* Assume no debug */
  67.     rcs_only = FALSE;        /* Assume check for local file first */
  68.  
  69.     if (argc > 1) {
  70.     for (j = 1; j < 3; j++) {
  71.         if (*argv[j] == '-') {
  72.         if (argv[j][1] == 'd') {
  73.             debug = TRUE;
  74.             i++;
  75.         } else if (argv[j][1] == 'r') {
  76.             rcs_only = TRUE;
  77.             i++;
  78.         } else {
  79.             fprintf (stderr,
  80.                  "Unrecognized option %s ignored\n", argv[j]);
  81.         }
  82.         }
  83.     }
  84.     }
  85.  
  86.     /*----
  87.      * Make sure there are enough parameters.
  88.      *----*/
  89.  
  90.     if (argc < i) {
  91.     fprintf (stderr,
  92.          "Usage: chktime [-d] [-r] <source-file> <object-file>\n");
  93.     exit (-99);
  94.     }
  95.  
  96.     /*----
  97.      * Make sure we can get the information about the object
  98.      * file.  Here we also get the information.
  99.      *----*/
  100.  
  101.     if (stat (argv[i-1], &o_stat) < 0) {
  102.     fprintf (stderr, "Object file error %d\n", errno);
  103.     exit (-99);
  104.     }
  105.  
  106.     /*----
  107.      * Check if we can get the information about the file in
  108.      * the local directory.  If not, start processing RCS file.
  109.      *----*/
  110.  
  111.     if (rcs_only || stat (argv[i-2], &s_stat) < 0) {
  112.  
  113.         /*----
  114.          * Create a file name in cbuff which consists of the
  115.          * path RCS/ followed by the file name with ,v appended
  116.          * to it.
  117.          *----*/
  118.  
  119.     strcpy (cbuff, "RCS/");
  120.     strcat (cbuff, argv[i-2]);
  121.     strcat (cbuff, ",v");
  122.  
  123.     if (debug)
  124.         printf ("chktime: Opening RCS file %s\n", cbuff);
  125.  
  126.     if ((fp = fopen (cbuff, "r")) == NULL) {
  127.         fprintf (stderr, "Source file error %d on %s\n", errno, cbuff);
  128.         exit (-99);
  129.     }
  130.  
  131.     while (strncmp (dbuff, "date", 4) != 0) {
  132.         if (fgets (dbuff, 256, fp) == NULL) {
  133.         fprintf (stderr, "Error reading RCS file %s - 1\n", cbuff);
  134.         exit (-99);
  135.         }
  136.     }
  137.  
  138.     fclose (fp);
  139.     cpos = &dbuff[4];
  140.     while (isspace (*cpos))
  141.         cpos++;
  142.  
  143.     if (debug)
  144.         printf ("         Found date line:           %.17s\n", cpos);
  145.  
  146.     tmptr = localtime (&o_stat.st_mtime);
  147.     ascftime (cbuff, "%y.%m.%d.%H.%M.%S", tmptr);
  148.  
  149.     if (debug)
  150.         printf ("         Converted time for object: %s\n", cbuff);
  151.  
  152.     i = strncmp (cpos, cbuff, 17);
  153.     i /= (i > 0) ? i : -i;
  154.     } else {
  155.  
  156.     if (debug) {
  157.         printf ("chktime: Found local file %s\n", argv[i-2]);
  158.         printf ("         Modify time of source = %ld\n", s_stat.st_mtime);
  159.         printf ("         Modify time of object = %ld\n", o_stat.st_mtime);
  160.     }
  161.  
  162.     i = (s_stat.st_mtime < o_stat.st_mtime) ? -1 :
  163.         (s_stat.st_mtime > o_stat.st_mtime) ?  1 : 0;
  164.     }
  165.  
  166.     if (debug)
  167.     printf ("         Returning a value of %d\n", i);
  168.  
  169.     exit (i);
  170. }
  171.